#!/bin/tcsh -ef
#############################################################################
#                                                                           #
# Title: Refresh Databases                                                  #
#                                                                           #
# (C) 2dx.org, GNU Plublic License.                                         #
#                                                                           #
# Created..........: 03/01/2006                                             #
# Last Modification: 03/01/2006                                             #
# Author...........: 2dx.org                                                #
#                                                                           #
#############################################################################
#
# SORTORDER: 25
#
# MANUAL: This script will refresh the 2dx_image.cfg files in the image directories. 
#
# MANUAL: In order to do so, it will read the config files in all selected image directories and save them again. This can be useful if a variable has been added to the master config file 2dx_master.cfg, which then should be updated into all local image directory 2dx_image.cfg files.
#
# You can also adapt this script and add your own function, which will then be applied to all selected images.
#
#$end_local_vars
#
set proc_2dx = ""
set bin_2dx = ""
set app_2dx_image = ""
#
#$end_vars
#
set scriptname = 2dx_refreshData
\rm -f LOGS/${scriptname}.results
#
set ccp4_setup = 'y'
source ${proc_2dx}/initialize
#
source ${proc_2dx}/2dx_makedirs 
#
echo "<<@progress: 1>>"
#
cat 2dx_merge_dirfile.dat | tr "\n" " " > SCRATCH/2dx_merge_dirfile_oneline.dat
set dirlist = "`cat SCRATCH/2dx_merge_dirfile_oneline.dat`"
#
set oldir = $PWD
#
cd ..
find . -name 2dx_image.cfg -print | sed 's/\/2dx_image.cfg//' > ${oldir}/2dx_merge_alldirs.dat
cd ${oldir}
#
set dirnum = `cat 2dx_merge_alldirs.dat | wc -l`
echo ": Will work on ${dirnum} image directories."
set increment = `echo ${dirnum} | awk '{ s = 70.0 / ( $1 + 1 ) } END { print s }'`
set curval = 20.0  
#
set dircounter = ${dirnum}
while ( ${dircounter} > 0 )
  #
  ${bin_2dx}/2dx_getline.exe << eot >> TMP.tmp
${oldir}/2dx_merge_alldirs.dat
${dircounter}
eot
  #
  set from_dir = `cat TMP.tmp`
  \rm TMP.tmp
  #
  cd ${oldir}
  cd ..
  cd ${from_dir}
  #
  set fullpath = `pwd`
  #
  echo "::Working on ${from_dir}"
  #
  # source ${proc_2dx}/initialize
  #
  ${app_2dx_image} ${fullpath} "2dx_initialize"
  #
  cd ${oldir}
  #
  set curval = `echo ${curval} ${increment} | awk '{ s = $1 + $2 } END { print s }'`
  set curprogress = `echo ${curval} | awk '{ s = int( $1 ) } END { print s }'`
  echo "<<@progress:${curprogress}>>"
  #
  @ dircounter -= 1
  #
end
#
echo "<<@progress: 100>>"
${proc_2dx}/linblock "${scriptname} finished correctly."
#
